package com.example.smartcity.ui.act

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity.R
import com.example.smartcity.adapter.SeatListAdapter
import com.example.smartcity.app.App
import com.example.smartcity.http.HlResp
import com.example.smartcity.http.Seat
import kotlinx.android.synthetic.main.activity_hlxz.*
import kotlinx.android.synthetic.main.layout_app_bar_common.*

/**
 * 值机选座
 */
class HlxzActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hlxz)
        tvAppBarTitle.text = "值机选座"
        ivAppBarLeft.setOnClickListener { finish() }
        loadSeatData()
    }

    // 读取仓位数据
    private fun loadSeatData() {
        App.fetchData<HlResp>(
            "/prod-api/system/space/list?airAircraftId=1",
            HlResp::class.java
        ) {
            //仓位按照行列排序
            val seats =
                it.rows.sortedWith(compareBy({ seat -> seat.clo.toInt() }, { seat -> seat.row }))
            Log.e("TAG", "loadSeatData: $seats")
            val linearLayoutManager = LinearLayoutManager(this)
            linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
            rvHlxz.layoutManager = linearLayoutManager
            rvHlxz.adapter = SeatListAdapter(seats,
                object : SeatListAdapter.OnItemClickListener {
                    override fun onClick(seat: Seat) {
                        var dialog: AlertDialog = AlertDialog.Builder(this@HlxzActivity)
                            .setIcon(R.mipmap.ic_launcher_round)
                            .setTitle("值机选座")
                            .setMessage("clo:${seat.clo},row:${seat.row},state:${seat.state}")
                            .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                            .setPositiveButton("确定") { dialog, _ -> dialog.dismiss() }
                            .create()
                        dialog.show()
                    }
                })
        }
    }

}
